//
//  TopNavigationBar.m
//  TopNavigationBar
//
//  Created by bosma on 14-4-8.
//  Copyright (c) 2014年 cn.com.bosma. All rights reserved.
//

#import "TopNavigationBar.h"


@interface TopNavigationBar ()

@property (nonatomic, assign)CGRect backgroundImgViewFmr;
@property (nonatomic, strong)NSString *backgroundImgName;
@property (nonatomic, strong)NSString *labelTitleStr;
@property (nonatomic, assign)CGRect labelFrm;

@property (nonatomic, assign)BOOL haveLftBtn;
@property (nonatomic, assign)CGRect lftBtnFrm;
@property (nonatomic ,strong)NSString *lftBtnImgName;

@property (nonatomic, assign)BOOL haveRhtBtn;
@property (nonatomic, assign)CGRect rhtBtnFrm;
@property (nonatomic ,strong)NSString *rhtBtnImgName;

@end

@implementation TopNavigationBar
@synthesize backgroundImgViewFmr = _backgroundImgViewFmr;
@synthesize backgroundImgName = _backgroundImgName;
@synthesize labelTitleStr = _labelTitleStr;
@synthesize labelFrm = _labelFrm;
@synthesize haveLftBtn = _haveLftBtn;
@synthesize lftBtnFrm = _lftBtnFrm;
@synthesize lftBtnImgName = _lftBtnImgName;
@synthesize haveRhtBtn = _haveRhtBtn;
@synthesize rhtBtnFrm = _rhtBtnFrm;
@synthesize rhtBtnImgName = _rhtBtnImgName;


//重写initWithFrame方法
- (id)initWithFrame:(CGRect)frame BgImageName:(NSString *)bgimgName labelTitle:(NSString *)title LabFrame:(CGRect)labFrm LeftBool:(BOOL)haveLeftBtn LeftBtnFrame:(CGRect)leftBtnFrm LeftBtnImgName:(NSString *)leftBtnImgName RightBool:(BOOL)haveRightBtn RightBtnFrame:(CGRect)rightBtnFrm RightBtnImgName:(NSString *)rightBtnImgName
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundImgViewFmr = frame;
        self.backgroundImgName    = bgimgName;
        
        self.labelTitleStr        = title;
        self.labelFrm             = labFrm;
        
        self.haveLftBtn           = haveLeftBtn;
        self.lftBtnFrm            = leftBtnFrm;
        self.lftBtnImgName        = leftBtnImgName;
        
        self.haveRhtBtn           = haveRightBtn;
        self.rhtBtnFrm            = rightBtnFrm;
        self.rhtBtnImgName        = rightBtnImgName;
        
        //创建背景图片
        [self createBackgroundImageView];
        
        //创建label标题
        [self createTitleLabel];
        
        //创建barButtonItem
        [self createBarButtonItem];
    }
    return self;
}

/**
 *	@brief	创建背景图片
 */
- (void)createBackgroundImageView
{
    UIImageView *bgImgView = [[UIImageView alloc]initWithFrame:self.backgroundImgViewFmr];
    bgImgView.image = [UIImage imageNamed:self.backgroundImgName];
    [self addSubview:bgImgView];
    
}

/**
 *	@brief	创建Label标题
 */
- (void)createTitleLabel
{
    UILabel *label = [[UILabel alloc]initWithFrame:self.labelFrm];
    label.text = self.labelTitleStr;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:20.0f];
    label.textColor = [UIColor whiteColor];
    [self addSubview:label];
    
}

/**
 *	@brief	创建barButtonItem
 */
- (void)createBarButtonItem
{
    if (self.haveLftBtn) {
        //创建左侧按钮
        UIButton *leftClickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftClickBtn.frame = self.lftBtnFrm;
        [leftClickBtn setImage:[UIImage imageNamed:self.lftBtnImgName] forState:UIControlStateNormal];
        [leftClickBtn setTag:10001];
        [leftClickBtn addTarget:self action:@selector(navBarBtnItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:leftClickBtn];
        
    }
    if (self.haveRhtBtn) {
        //创建右侧按钮
        UIButton *rightClickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightClickBtn.frame = self.rhtBtnFrm;
        [rightClickBtn setImage:[UIImage imageNamed:self.rhtBtnImgName] forState:UIControlStateNormal];
        [rightClickBtn setTag:10002];
        [rightClickBtn addTarget:self action:@selector(navBarBtnItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rightClickBtn];
        
    }
}

/**
 *	@brief	按钮的点击事件
 *
 *	@param 	sender 	传递的参数
 */
- (void)navBarBtnItemClicked:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    switch (btn.tag) {
        case 10001:
            if ([self.delegate respondsToSelector:@selector(itemButtonClicked:)]) {
                [self.delegate itemButtonClicked:0];
            }
            break;
        case 10002:
            if ([self.delegate respondsToSelector:@selector(itemButtonClicked:)]) {
                [self.delegate itemButtonClicked:1];
            }
            break;
            
        default:
            break;
    }
}




@end
